package com.ming.stringstudy.feedbacksystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.stringstudy.feedbacksystem.anno.PermissionLog;
import com.ming.stringstudy.feedbacksystem.entity.User;
import com.ming.stringstudy.feedbacksystem.mapper.UserMapper;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.stringstudy.feedbacksystem.utils.PermissionConstants;
import com.ming.stringstudy.feedbacksystem.utils.PermissionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aming
 * @since 2025-01-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int register(User user) {

        // 检查账号是否已存在
        if (userMapper.findByAccount(user.getAccount()) != null) {
            return 0;
        }

        // 加密密码
        String encryptedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encryptedPassword);

        // 设置创建时间和更新时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 保存用户到数据库
        int i = userMapper.insert(user);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User login(User user) {
        User u = userMapper.findByAccount(user.getAccount());
        if (u == null){
            return null;
        }
        if (!passwordEncoder.matches(user.getPassword(), u.getPassword())){
            return null;
        }
        return u;
    }
    @PermissionLog(PermissionConstants.USER_QUERY)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageBean<User> page(Integer page, Integer pageSize, User user) {
//        PageHelper.startPage(page, pageSize);
//
//        List<User> UserList = userMapper.findByAttributes(user);
//        // 封装到PageBean中
//        Page<User> p = (Page<User>) UserList;
//        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
//        return pageBean;
        // 开始分页
        PageHelper.startPage(page, pageSize);

        // 查询用户列表
        List<User> userList = userMapper.findByAttributes(user);

        // 获取分页信息
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        // 封装到 PageBean 中
        PageBean<User> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return pageBean;
    }
    @PermissionLog(PermissionConstants.USER_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addUser(User user){
        // 检查账号是否已存在
        if (userMapper.findByAccount(user.getAccount()) != null) {
            return 0;
        }

        // 加密密码
        String encryptedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encryptedPassword);

        // 设置创建时间和更新时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 保存用户到数据库
        int result = userMapper.insert(user);
        return result;
    }
    @PermissionLog(PermissionConstants.USER_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(User user) {
        if (userMapper.findByAccount(user.getAccount()) != null) {
            return 0;
        }
        if (user.getPassword() != null) {
            String encryptedPassword = passwordEncoder.encode(user.getPassword());
            user.setPassword(encryptedPassword);
        }
        user.setUpdateTime(LocalDateTime.now());
        int result = userMapper.updateById(user);
        return result;
    }
    @PermissionLog(PermissionConstants.USER_MANAGE)
    @Override
    public int deleteUser(List<Integer> ids) {
        int result = userMapper.deleteBatchIds(ids);
        return result;
    }

}
